using System;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class UGUIScrollRect : ScrollRect
{
    public enum Mode
    {
        /// <summary>
        /// 原始状态
        /// </summary>
        None,
        /// <summary>
        /// 网格
        /// </summary>
        Grid,
        /// <summary>
        /// 动态
        /// </summary>
        Dynamic,
    }

    public enum DynamicMode
    {
        /// <summary>
        /// 静态大小
        /// </summary>
        Static,
        /// <summary>
        /// 动态高度
        /// </summary>
        DynamicHeight,
    }

    public Mode _tempMode;
    public Mode m_Mode;
    public DynamicMode m_DynamicMode;

    public Action<GameObject, int> OnInitializeItem;
    public Action<GameObject, int> OnSelectedItem;
    public Action OnBeginDragEvt;
    public Action OnEndDragEvt;

    private LayoutGroup m_LayoutGroup;
    // 注意：UGUIGrid 和 UGUIDynamicGrid 在当前项目中不存在，相关功能已暂时禁用
    // private UGUIGrid m_UGUIGrid;
    // private UGUIDynamicGrid m_UGUIDynamicGrid;

    public bool isThrough= false;
    [SerializeField]
    private UGUIScrollRect parentScroll;


#if  UNITY_EDITOR

    protected override void OnValidate()
    {
        base.OnValidate();

        if (_tempMode != m_Mode)
        {
            _tempMode = m_Mode;
            GenerateGrid();
        }
    }

#endif

    public UGUIScrollRect GetParentScroll()
    {
        Transform t = transform;
        while (t.parent != null)
        {
            t = t.parent;
            UGUIScrollRect scr = t.GetComponent<UGUIScrollRect>();
            if (scr != null)
            {
                return scr;
            }
        }
        return null;
    }

    private void GenerateGrid()
    {
        // 注意：UGUIGrid 和 UGUIDynamicGrid 在当前项目中不存在，此功能已暂时禁用
        // 如果需要使用此功能，请先添加 UGUIGrid 和 UGUIDynamicGrid 类
        /*
        switch (_tempMode)
        {
            case Mode.None:
                m_UGUIGrid = gameObject.GetGameComponent<UGUIGrid>();
                if (m_UGUIGrid != null)
                {
                    StartCoroutine(Utility.Coroutine.Delay(0.1f, () => gameObject.RemoveComponent<UGUIGrid>(true)));
                    m_UGUIGrid = null;
                }
                m_UGUIDynamicGrid = gameObject.GetGameComponent<UGUIDynamicGrid>();
                if (m_UGUIDynamicGrid != null)
                {
                    StartCoroutine(Utility.Coroutine.Delay(0.1f, () => gameObject.RemoveComponent<UGUIDynamicGrid>(true)));
                    m_UGUIDynamicGrid = null;
                }
                break;
            case Mode.Grid:
                m_UGUIDynamicGrid = gameObject.GetGameComponent<UGUIDynamicGrid>();
                if (m_UGUIDynamicGrid != null)
                {
                    StartCoroutine(Utility.Coroutine.Delay(0.1f, () => gameObject.RemoveComponent<UGUIDynamicGrid>(true)));
                    m_UGUIDynamicGrid = null;
                }
                m_UGUIGrid = gameObject.GetOrAddGameComponent<UGUIGrid>();
                m_UGUIGrid.GameMonoBehaviourName = nameof(UGUIGrid);
                break;
            case Mode.Dynamic:
                m_UGUIGrid = gameObject.GetGameComponent<UGUIGrid>();
                if (m_UGUIGrid != null)
                {
                    StartCoroutine(Utility.Coroutine.Delay(0.1f, () => gameObject.RemoveComponent<UGUIGrid>(true)));
                    m_UGUIGrid = null;
                }
                m_UGUIDynamicGrid = gameObject.GetOrAddGameComponent<UGUIDynamicGrid>();
                m_UGUIDynamicGrid.GameMonoBehaviourName = nameof(UGUIDynamicGrid);
                break;
            default:
                break;
        }
        */
    }

    /// <summary>
    /// 上次拖曳的Content 的Y值
    /// </summary>
    public float RecordLastDrag_Content_Y { get; private set; }

    public override void OnDrag(PointerEventData eventData)
    {
        base.OnDrag(eventData);
        if (isThrough && parentScroll != null)
        {
            ExecuteEvents.Execute(parentScroll.gameObject, eventData, ExecuteEvents.dragHandler);
        }
        RecordLastDrag_Content_Y = content.localPosition.y;
    }

    public override void OnEndDrag(PointerEventData eventData)
    {
        base.OnEndDrag(eventData);
        if (isThrough && parentScroll != null)
        {
            ExecuteEvents.Execute(parentScroll.gameObject, eventData, ExecuteEvents.endDragHandler);
        }
        RecordLastDrag_Content_Y = 0;
        OnEndDragEvt?.Invoke();
    }

    public override void OnBeginDrag(PointerEventData eventData)
    {
        base.OnBeginDrag(eventData);
        OnBeginDragEvt?.Invoke();
        if (isThrough)
        {
            if (parentScroll == null)
            {
                parentScroll = GetParentScroll();
            }
            if (parentScroll != null)
            {
                ExecuteEvents.Execute(parentScroll.gameObject, eventData, ExecuteEvents.beginDragHandler);
            }
        }
    }


    #region 屏蔽 ScrollRect 的扩展功能

    //public void Init(Mode mode, DynamicMode dynamicMode = DynamicMode.Static)
    //{
    //    m_Mode = mode;
    //    m_DynamicMode = dynamicMode;

    //    m_LayoutGroup = content.GetComponent<LayoutGroup>();

    //    switch (mode)
    //    {
    //        case Mode.Grid:
    //            if (m_UGUIGrid == null)
    //            {
    //                m_UGUIGrid = gameObject.GetOrAddGameComponent<UGUIGrid>();
    //                m_UGUIGrid.Init(this, m_LayoutGroup);
    //                m_UGUIGrid.OnInitializeItem = OnInitializeItem;
    //                m_UGUIGrid.OnSelectedItem = OnSelectedItem;
    //            }

    //            break;
    //        case Mode.Dynamic:
    //            if (m_UGUIDynamicGrid == null)
    //            {
    //                m_UGUIDynamicGrid = gameObject.GetOrAddGameComponent<UGUIDynamicGrid>();
    //                m_UGUIDynamicGrid.Init(this, m_DynamicMode);
    //                m_UGUIDynamicGrid.OnInitializeItem = OnInitializeItem;
    //                m_UGUIDynamicGrid.OnSelectedItem = OnSelectedItem;
    //            }
    //            break;
    //        default:
    //            break;
    //    }
    //}

    //public void SetSize(int size, bool isDelay = false)
    //{
    //    switch (m_Mode)
    //    {
    //        case Mode.Grid:
    //            if (m_UGUIGrid == null)
    //            {
    //                m_UGUIGrid = gameObject.GetOrAddGameComponent<UGUIGrid>();
    //                m_UGUIGrid.Init(this, m_LayoutGroup);
    //                m_UGUIGrid.OnInitializeItem = OnInitializeItem;
    //                m_UGUIGrid.OnSelectedItem = OnSelectedItem;
    //            }
    //            m_UGUIGrid.SetSize(size, isDelay);
    //            break;
    //        case Mode.Dynamic:
    //            if (m_UGUIDynamicGrid == null)
    //            {
    //                m_UGUIDynamicGrid = gameObject.GetOrAddGameComponent<UGUIDynamicGrid>();
    //                m_UGUIDynamicGrid.Init(this, m_DynamicMode);
    //                m_UGUIDynamicGrid.OnInitializeItem = OnInitializeItem;
    //                m_UGUIDynamicGrid.OnSelectedItem = OnSelectedItem;
    //            }
    //            m_UGUIDynamicGrid.SetSize(size);
    //            break;
    //        default:
    //            break;
    //    }
    //}

    //public void Refresh()
    //{
    //    switch (m_Mode)
    //    {
    //        case Mode.Grid:
    //            if (m_UGUIGrid == null)
    //            {
    //                m_UGUIGrid = gameObject.GetOrAddGameComponent<UGUIGrid>();
    //                m_UGUIGrid.Init(this, m_LayoutGroup);
    //                m_UGUIGrid.OnInitializeItem = OnInitializeItem;
    //                m_UGUIGrid.OnSelectedItem = OnSelectedItem;
    //            }
    //            m_UGUIGrid.Refresh();
    //            break;
    //        case Mode.Dynamic:
    //            if (m_UGUIDynamicGrid == null)
    //            {
    //                m_UGUIDynamicGrid = gameObject.GetOrAddGameComponent<UGUIDynamicGrid>();
    //                m_UGUIDynamicGrid.Init(this, m_DynamicMode);
    //                m_UGUIDynamicGrid.OnInitializeItem = OnInitializeItem;
    //                m_UGUIDynamicGrid.OnSelectedItem = OnSelectedItem;
    //            }
    //            m_UGUIDynamicGrid.RefreshItemStatus();
    //            break;
    //    }
    //}

    #endregion
}

